home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / util / wb / peelnewtools.lha / PeelNewTools.c < prev    next >
C/C++ Source or Header  |  1995-09-09  |  4KB  |  204 lines

  1. #define ARRAYSIZE 1000
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6.  
  7. #include <exec/types.h>
  8. #include <exec/memory.h>
  9. #include <libraries/dos.h>
  10. #include <clib/exec_protos.h>
  11. #include <clib/dos_protos.h>
  12. #include <clib/icon_protos.h>
  13. #include <dos/rdargs.h>
  14.  
  15. char version[]="VER: PeelNewTools V1.0▀";
  16. char filename[40];
  17. char longdirname[160]="";
  18. char template[]="File/A,S=Single/S,R=Recursive/S,L=Log/S";
  19. LONG argarray[4];
  20. char **newtools;
  21. struct RDArgs *rdargs;
  22.  
  23. BOOL infoname(char *name)
  24. {
  25.     int length=strlen(name);
  26.  
  27.     if(strlen(name)<5)
  28.         return(FALSE);
  29.     if(stricmp(name+length-5,".info"))
  30.         return(FALSE);
  31.     return(TRUE);
  32. }
  33.  
  34. void pferror( void )
  35. {
  36.         PrintFault(IoErr(),"ERROR: ");
  37. }
  38.  
  39. void safeexit(int x)
  40. {
  41.     if(newtools)
  42.         FreeMem(newtools,ARRAYSIZE);
  43.  
  44.     FreeArgs(rdargs);
  45.     exit(x);
  46. }
  47.  
  48. int cleantools(char *filename)
  49. {
  50.     struct DiskObject *icon;
  51.     char **tools1,**tools2,**oldtools;
  52.     int error=0;
  53.  
  54.     if(!(icon=GetDiskObject(filename))) {
  55.         pferror();
  56.         return(5);
  57.     }
  58.  
  59.     if(tools1=oldtools=icon->do_ToolTypes) {
  60.         tools2=newtools;
  61.  
  62.         while(*tools1)
  63.             if(stpblk(*tools1)[0]==0)
  64.                 tools1++;
  65.             else
  66.                 if(!(strncmp(*tools1,"IM1=",4))) {
  67.                     tools1++;
  68.                 }
  69.                 else
  70.                     if(!(strncmp(*tools1,"IM2=",4))) {
  71.                         tools1++;
  72.                     }
  73.                     else
  74.                         if(!(strcmp(*tools1,"*** DON'T EDIT THE FOLLOWING LINES!! ***"))) {
  75.                             tools1++;
  76.                         }
  77.                         else
  78.                             *(tools2++)=*(tools1++);
  79.  
  80.         *tools2=NULL;
  81.  
  82.         icon->do_ToolTypes=newtools;
  83.         if(!(PutDiskObject(filename,icon))) {
  84.             pferror();
  85.             error=5;
  86.         }
  87.  
  88.         icon->do_ToolTypes=oldtools;
  89.     }
  90.  
  91.     FreeDiskObject(icon);
  92.     return(error);
  93. }
  94.  
  95. void exitscan(BPTR oldlock,BPTR dirlock,struct FileInfoBlock *fblock)
  96. {
  97.     if(oldlock)
  98.         CurrentDir(oldlock);
  99.     FreeMem(fblock,sizeof(struct FileInfoBlock));
  100.     UnLock(dirlock);
  101. }
  102.  
  103. int scandir(char *dirname)
  104. {
  105.     BPTR    dirlock,oldlock=NULL;
  106.     struct FileInfoBlock *fiblock=NULL;
  107.     char *fname;
  108.     int error=0,errror;
  109.     int longlength;
  110.  
  111.     if(!(dirlock=Lock(dirname,ACCESS_READ))) {
  112.         pferror();
  113.         return(5);
  114.     }
  115.  
  116.     if(!(fiblock = AllocMem( sizeof( struct FileInfoBlock ),
  117.                                     MEMF_PUBLIC | MEMF_CLEAR ))) {
  118.         UnLock(dirlock);
  119.         PutStr("ERROR: Out of memory.\n");
  120.         return(20);
  121.     }
  122.  
  123.     if(!(Examine(dirlock,fiblock))) {
  124.         pferror();
  125.         exitscan(NULL,dirlock,fiblock);
  126.         return(5);
  127.     }
  128.  
  129.     if(fiblock->fib_DirEntryType<=0) {
  130.         printf("ERROR: %s is not a directory.\n",dirname);
  131.         exitscan(NULL,dirlock,fiblock);
  132.         return(10);
  133.     }
  134.  
  135.     strcat(longdirname,dirname);
  136.     longlength=strlen(longdirname);
  137.     if(longdirname[longlength-1]!='/') {
  138.         longdirname[longlength++]='/';
  139.         longdirname[longlength]='\0';
  140.     }
  141.  
  142.     oldlock=CurrentDir(dirlock);
  143.  
  144.     while(ExNext(dirlock,fiblock)) {
  145.         fname=fiblock->fib_FileName;
  146.         if((fiblock->fib_DirEntryType>0)&&argarray[2]) {
  147.             errror=scandir(fname);
  148.             if(errror==20) {
  149.                 PutStr("CleanInfo aborted..\n");
  150.                 exitscan(oldlock,dirlock,fiblock);
  151.                 return(20);
  152.             }
  153.             error=(errror>error)? errror : error;
  154.         }
  155.         else
  156.             if(infoname(fname)) {
  157.                 if(argarray[3])
  158.                     printf("%s%s...\n",longdirname,fname);
  159.                 fname[strlen(fname)-5]=0;
  160.                 error=(cleantools(fname)>error)? 5 : error;
  161.             }
  162.     }
  163.     longdirname[strlen(longdirname)-strlen(dirname)]=0;
  164.     exitscan(oldlock,dirlock,fiblock);
  165.     return(error);
  166. }
  167.  
  168. int main(int argc, char *argv[])
  169. {
  170.     int error=0;
  171.  
  172.     if(!(newtools=(char **)AllocMem(ARRAYSIZE,MEMF_ANY))) {
  173.         PutStr("ERROR: Out of memory.\n");
  174.         safeexit(20);
  175.     }
  176.  
  177.     if(!(rdargs=ReadArgs(template,argarray,NULL)))
  178.         safeexit(5);
  179.  
  180.     strncpy(filename,(char *)argarray[0],39);
  181.  
  182.     if(argarray[1]) {
  183.         if(infoname(filename))
  184.             filename[strlen(filename)-5]=0;
  185.         error=cleantools(filename);
  186.         if(argarray[3])
  187.             printf("\nCleaning %s.info\n\n",filename);
  188.     }
  189.     else {
  190.         if(argarray[3])
  191.             PutStr("\nCleaning files:\n");
  192.         error=scandir(filename);
  193.         if(argarray[3]) {
  194.             PutStr("\nDone..\n\n");
  195.             if(error)
  196.                 PutStr("Operation not entirely successful.\n\n");
  197.             else
  198.                 PutStr("Operation successful.\n\n");
  199.         }
  200.     }
  201.  
  202.     safeexit(error);
  203. }
  204.